package com.gljx.web.controller.back.process;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.ActiveProcess;
import com.gljx.web.Entity.ActiveProcessEntry;
import com.gljx.web.service.ProcessEntryService;
import com.gljx.web.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class processController {

    @Autowired
    private ProcessService processService;

    @Autowired
    private ProcessEntryService processEntryService;

    /**
     * 查询南昌驾校流程
     * @param request
     * @Created By zyq.
     * @return
     */
    @RequestMapping(value = "/selProcessByCompanyNc/look.action")
    public String selProcessByCompanyNc(HttpServletRequest request) {
        HttpSession session = request.getSession();
                    session.setAttribute("company","Nc");
        return "/backManage/process/process";
    }

    /**
     * ajax查询流程
     * @return
     * @Created By zyq.
     */
    @RequestMapping(value = "/selProcessNc")
    @ResponseBody
    public Map selProcessNc() {
        List<ActiveProcess> processes = processService.selProcessByCompany(ActivitiKey.Driving_NC_ID);
        Map map = new HashMap();
            map.put("rows",processes);
            map.put("total",processes.size());
        return map;
    }

    /**
     * 查询国力
     * @param request
     * @return
     */
    @RequestMapping(value = "/selProcessByCompanyGl/look.action")
    public String selProcessByCompanyGl(HttpServletRequest request) {
        HttpSession session = request.getSession();
                    session.setAttribute("company","Gl");
        return "/backManage/process/process";
    }

    /**
     * ajax查询国力流程
     * @return
     */
    @RequestMapping(value = "/selProcessGl")
    @ResponseBody
    public Map selProcessGl() {
        List<ActiveProcess> processes = processService.selProcessByCompany(ActivitiKey.Driving_GL_ID);
        Map map = new HashMap();
        map.put("rows",processes);
        map.put("total",processes.size());
        return map;
    }

    /**
     * 修改流程经办人
     * @param request
     * @param fid
     * @return
     */
    @RequestMapping(value = "/selAssginee")
    public String selAssginee(HttpServletRequest request,String fid,String flag) {
        List<ActiveProcessEntry> activeProcessEntryList = processEntryService.selProcessEntryByFid(fid);
        if (!activeProcessEntryList.isEmpty()) {
            ActiveProcessEntry activeProcessEntry = processEntryService.selProcessEntryByFid(fid).get(0);
            request.setAttribute("list", activeProcessEntry);
            request.setAttribute("parentId", fid);
        }
        request.setAttribute("flag", 2);
        request.setAttribute("type",flag);
        return "/backManage/process/assignee";
    }

    /**
     * 增加流程经办人
     * @param request
     * @param fid
     * @return
     */
    @RequestMapping(value = "/addAssginee")
    public String addAssginee(HttpServletRequest request,String fid,String flag) {
        List<ActiveProcessEntry> activeProcessEntryList = processEntryService.selProcessEntryByFid(fid);
        if (!activeProcessEntryList.isEmpty()) {
            ActiveProcessEntry activeProcessEntry = activeProcessEntryList.get(0);
            request.setAttribute("list", activeProcessEntry);
        }
        request.setAttribute("parentId", fid);
        request.setAttribute("flag",1);
        request.setAttribute("type",flag);
        return "/backManage/process/assignee";
    }

    /**
     * 保存流程经办人
     * @param request
     * @param activeProcessEntry
     * @return
     */
    @RequestMapping(value = "/saveAssginee")
    public String saveAssginee(HttpServletRequest request,ActiveProcessEntry activeProcessEntry) {
        activeProcessEntry.setFid(GetFid.UUIDIntoFid());
        HttpSession session = request.getSession();
        processEntryService.saveActiveProcessEntry(activeProcessEntry);
        request.setAttribute("type",true);
        request.setAttribute("company",session.getAttribute("company"));
        return "/backManage/process/process";
    }

    /**
     * 保存流程经办人
     * @param request
     * @param activeProcessEntry
     * @return
     */
    @RequestMapping(value = "/updateAssginee")
    public String updateAssginee(HttpServletRequest request,ActiveProcessEntry activeProcessEntry) {
        processEntryService.updateActiveProcessEntry(activeProcessEntry);
        HttpSession session = request.getSession();
        request.setAttribute("company",session.getAttribute("company"));
        request.setAttribute("type",true);
        return "/backManage/process/process";
    }

}
